<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-10
 * Time: 17:55
 */

namespace app\api\controller;
use app\model\UserModel;
use app\service\LogService;
use app\service\MsgService;
use app\service\SbPayService;
use app\service\ThirdService;
use app\service\WechatService;


use EasyWeChat\Kernel\Messages\NewsItem;
use think\Db;

class Test extends AllBase
{

  public function __construct () {
    parent::__construct();
  }

  public function index () {
    ThirdService::S()->processing(16);
  }

    public function resetTemp(){
        //配置模板消息
        MsgService::S([
            'mall_id' => $this->mall['id']
        ])->resetMallTmpl($this->mall['id']);

    }

    public function test(){
        $where['zkhb_loginstatus.update_time']=array("EGT",time()-3600*24);
        //Loginstatus::getAll(,'','uid');
        $recentList = Db::name("user_wechat")
            ->field("zkhb_user_wechat.uid,zkhb_user_wechat.openid")
            ->join("zkhb_loginstatus","zkhb_user_wechat.uid=zkhb_loginstatus.uid")
            ->where($where)
            ->select();
        dump($recentList);die;
        /*$moneyLog['uid']="";
        $recentList = \app\common\model\User::getRencentList($moneyLog['uid']);
        $userInfo['nickname']="刘洪";
        $userInfo['mall_id']="1";
        $obj = WechatService::S([
            'mall_id' => $userInfo['mall_id']
        ]);
        if($recentList) {
            foreach($recentList as $v) {
                $obj->send(
                    2,
                    \app\common\model\User::uid2openid($v['uid']),
                    [new NewsItem([
                        'title'       => $userInfo['nickname']."在群内发了一个红包,快来抢红包吧",
                        'description' => '',
                        'url'         => request()->domain()."/m/chat/pages/index/index?rid=".$moneyLog['id']."&mall_id={$userInfo['mall_id']}",
                        'image'       => "http://cdn.1s1s.club/shopcom/20190510/1725488242.png",
                    ])]
                );
            }
        }*/
    }


    //异步发送客服消息
    public function kefu_script(){

        //@所有人
        $mall_id=$_POST['mall_id'];
        $type=$_POST['type'];
        $nickname=$_POST['nick_name'];

        $obj = WechatService::S([
            'mall_id' => $mall_id//$this->userInfo['mall_id']
        ]);


        LogService::S()->writeLog('re','data',$_POST);
        $where['zkhb_loginstatus.update_time']=array("EGT",time()-3600*24);
        //Loginstatus::getAll(,'','uid');
        $recentList = Db::name("user_wechat")
            ->field("zkhb_user_wechat.uid,zkhb_user_wechat.openid")
            ->join("zkhb_loginstatus","zkhb_user_wechat.uid=zkhb_loginstatus.uid")
            ->where($where)
            ->select();
        //LogService::S()->writeLog('user','info',$recentList);
        switch ($type){
            case "chat":
                $url = request()->domain()."/m/chat/pages/index/index?mall_id={$mall_id}";
                $content="急事，群主{$nickname}在群里对你说:<a href='{$url}'>点击查看说了些什么</a>";
                if($recentList) {
                    foreach($recentList as $v) {
                        $obj->send(
                            1,
                            $v['openid'],
                            $content
                        );
                    }
                }
                break;

            case "hongbao":
                $money_log_id=$_POST['money_log_id'];
                if($recentList) {
                    foreach($recentList as $v) {
                        $obj->send(
                            2,
                            \app\common\model\User::uid2openid($v['uid']),
                            [new NewsItem([
                                'title'       => $nickname."在群内发了一个红包,快来抢红包吧",
                                'description' => '快来抢红包，先到先得',
                                'url'         => request()->domain()."/m/chat/pages/index/index?rid=".$money_log_id."&mall_id={$mall_id}",
                                'image'       => "http://cdn.1s1s.club/shopcom/20190510/1725488242.png",
                            ])]
                        );
                    }
                }
                break;
        }
    }

    public function test1(){
        $host=get_host();
        //@所有人
        $array['mall_id']="1";//$this->userInfo['mall_id'];
        //$array['type']="chat";//$_GET['type'];
        $array['nick_name']="刘洪";//$this->userInfo['nickname'];//$_GET['nick_name'];
        //$array['url']="";
        //LogService::S()->writeLog('log','array',$host);
        $re=doRequest($host,'/api/test/kefu_script',$array);//异步通知
        LogService::S()->writeLog('re','data',$re);
        dump($re);die;
    }

}